﻿using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
using Unity;
using System;
using System.Linq;
using System.Text;

public class ItemScript : MonoBehaviour {

    public GameObject Items;
    public GameObject player;
    float diffTime = 5f;
    float startTime = 0.0f;
    float currentTime = 0.0f;

	// Use this for initialization
	void Start () {
        CreateItemObjects(Items);
	}

    public void CreateItemObjects(GameObject parent)
    {
        List<Coin> itemsSelected = ItemCreationScript.item.GetCoinItems();
        float yAxis = 5.0f;
        float xAxis = GetItemXAxis(itemsSelected.First().Location);
        foreach (Coin itemCoin in itemsSelected)
        {
            CreateCoin(parent, itemCoin, ref xAxis, ref yAxis);
            yAxis = yAxis + 0.5f;
        }
    }

    public void CreateCoin(GameObject parent, Coin item, ref float xAxis, ref float yAxis)
    {
        UnityEngine.Object itemPrefab = AssetDatabase.LoadAssetAtPath(GetItemPrefab(item.CoinType), typeof(GameObject));
        GameObject itemObject = Instantiate(itemPrefab, new Vector3(xAxis, yAxis, 0), Quaternion.identity) as GameObject;
        itemObject.rigidbody2D.gravityScale = 0.05f;
        itemObject.rigidbody2D.isKinematic = true;
        itemObject.tag = "itemTag";

        itemObject.transform.parent = parent.transform;
    }

    public string GetItemPrefab(string coinType)
    {
        if (coinType.Equals("1"))
        {
            return "Assets/Prefabs/Coin.prefab";
        }
        else
        {
            return "Assets/Prefabs/Diamond.prefab";
        }
    }

    public float GetItemXAxis(string location)
    {
        float x = 0;
        if (location == "1")
        {
            x = 3.4f;
        }
        else if (location == "2")
        {
            x = 1.7f;
        }
        else if (location == "3")
        {
            x = 0f;
        }
        else if (location == "4")
        {
            x = -1.7f;
        }
        else if (location == "5")
        {
            x = -3.4f;
        }

        return x;
    }

    private Vector2 speed = new Vector2(0, 1f);

    public void MoveRigidBody(GameObject[] gg)
    {
        //GameObject ground = GameObject.FindGameObjectWithTag ("Ground");
        //ground.rigidbody2D.MovePosition(ground.rigidbody2D.position - speed * Time.deltaTime);
        foreach (GameObject item in gg)
        {
            item.rigidbody2D.MovePosition(item.rigidbody2D.position - speed * Time.deltaTime);
            //Debug.Log(player.transform.position.y + " " + item.rigidbody2D.position.y);
            if (player.transform.position.y > item.rigidbody2D.position.y)
            {
                //BoxCollider2D bc2d = item.AddComponent<BoxCollider2D>();
                //BoxCollider2D bc2d = item.collider2D as BoxCollider2D;
                //bc2d.size = new Vector3(3.04f, 0.64f, 0);
            }
            else
            {
                Destroy(item.collider2D);
            }
        }
    }

    public void CreateAndDestroy(GameObject parent)
    {
        GameObject[] gg = GameObject.FindGameObjectsWithTag("itemTag");
        MoveRigidBody(gg);
        CreatePL(parent);
        foreach (GameObject g in gg)
        {
            if (g.transform.position.y < -4.0f)
            {
                GameObject.Destroy(g);
            }
        }
    }

    void CreatePL(GameObject parent)
    {
        currentTime = Time.time;
        if (PlatformCreationScript.gameStartTime > 1000.0f)
        {
            PlatformCreationScript.plats = PlatformCreationScript.plat.CreatePlatforms(1000);
        }
        if (currentTime - startTime > diffTime)
        {
            CreateItemObjects(Items);
            startTime = currentTime;
        }
    }
	
	// Update is called once per frame
	void Update () {
	
	}
}
